/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoginViewImpl.java
 *
 * Created on 15/02/2010, 22:37:44
 */

package tpdisenio2009.mvp.display;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ItemListener;
import java.awt.event.WindowListener;
import java.util.List;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;

/**
 *
 * @author DeuterioX
 */
public class LoginViewImpl extends javax.swing.JDialog implements LoginView {

    /** Creates new form LoginViewImpl */
    public LoginViewImpl(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        //this.setLocationRelativeTo(null);
        initComponents();
        getRootPane().setDefaultButton(jButtonLogin);
        Dimension dim = this.getToolkit().getScreenSize();
        this.setLocation((dim.width - this.getWidth()) / 2,(dim.height - this.getHeight()) / 2);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelDocumentType = new javax.swing.JLabel();
        jComboBoxDocumentType = new javax.swing.JComboBox();
        jLabelDocumentNumber = new javax.swing.JLabel();
        jTextFieldDocumentNumber = new javax.swing.JTextField();
        jPasswordFieldPassword = new javax.swing.JPasswordField();
        jLabelPassword = new javax.swing.JLabel();
        jButtonNewUser = new javax.swing.JButton();
        jButtonLogin = new javax.swing.JButton();
        jButtonPasswordRecover = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(tpdisenio2009.Tpdisenio2009App.class).getContext().getResourceMap(LoginViewImpl.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jLabelDocumentType.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelDocumentType.setText(resourceMap.getString("jLabelDocumentType.text")); // NOI18N
        jLabelDocumentType.setName("jLabelDocumentType"); // NOI18N

        jComboBoxDocumentType.setToolTipText(resourceMap.getString("jComboBoxDocumentType.toolTipText")); // NOI18N
        jComboBoxDocumentType.setName("jComboBoxDocumentType"); // NOI18N

        jLabelDocumentNumber.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelDocumentNumber.setText(resourceMap.getString("jLabelDocumentNumber.text")); // NOI18N
        jLabelDocumentNumber.setName("jLabelDocumentNumber"); // NOI18N

        jTextFieldDocumentNumber.setToolTipText(resourceMap.getString("jTextFieldDocumentNumber.toolTipText")); // NOI18N
        jTextFieldDocumentNumber.setName("jTextFieldDocumentNumber"); // NOI18N

        jPasswordFieldPassword.setToolTipText(resourceMap.getString("jPasswordFieldPassword.toolTipText")); // NOI18N
        jPasswordFieldPassword.setName("jPasswordFieldPassword"); // NOI18N

        jLabelPassword.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabelPassword.setText(resourceMap.getString("jLabelPassword.text")); // NOI18N
        jLabelPassword.setName("jLabelPassword"); // NOI18N

        jButtonNewUser.setText(resourceMap.getString("jButtonNewUser.text")); // NOI18N
        jButtonNewUser.setToolTipText(resourceMap.getString("jButtonNewUser.toolTipText")); // NOI18N
        jButtonNewUser.setDefaultCapable(false);
        jButtonNewUser.setName("jButtonNewUser"); // NOI18N

        jButtonLogin.setText(resourceMap.getString("jButtonLogin.text")); // NOI18N
        jButtonLogin.setName("jButtonLogin"); // NOI18N

        jButtonPasswordRecover.setText(resourceMap.getString("jButtonPasswordRecover.text")); // NOI18N
        jButtonPasswordRecover.setToolTipText(resourceMap.getString("jButtonPasswordRecover.toolTipText")); // NOI18N
        jButtonPasswordRecover.setDefaultCapable(false);
        jButtonPasswordRecover.setName("jButtonPasswordRecover"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButtonNewUser, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonPasswordRecover, javax.swing.GroupLayout.DEFAULT_SIZE, 155, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonLogin, javax.swing.GroupLayout.DEFAULT_SIZE, 89, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(28, 28, 28)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addComponent(jLabelDocumentNumber, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jLabelDocumentType)))
                            .addComponent(jLabelPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jTextFieldDocumentNumber, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addComponent(jComboBoxDocumentType, 0, 178, Short.MAX_VALUE))
                        .addGap(34, 34, 34))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jComboBoxDocumentType, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelDocumentType, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelDocumentNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextFieldDocumentNumber, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jPasswordFieldPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonNewUser)
                    .addComponent(jButtonPasswordRecover)
                    .addComponent(jButtonLogin))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonLogin;
    private javax.swing.JButton jButtonNewUser;
    private javax.swing.JButton jButtonPasswordRecover;
    private javax.swing.JComboBox jComboBoxDocumentType;
    private javax.swing.JLabel jLabelDocumentNumber;
    private javax.swing.JLabel jLabelDocumentType;
    private javax.swing.JLabel jLabelPassword;
    private javax.swing.JPasswordField jPasswordFieldPassword;
    private javax.swing.JTextField jTextFieldDocumentNumber;
    // End of variables declaration//GEN-END:variables

    public String getDocument() {
        return jTextFieldDocumentNumber.getText();
    }

    public String getPassword() {
        return String.valueOf( jPasswordFieldPassword.getPassword() );
    }

    public void setDocumentType( List<String> documentTypes) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        for ( String documentType : documentTypes) {
            model.addElement( documentType );
        }
        jComboBoxDocumentType.setModel( model );
    }

    public String getDocumentType() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) jComboBoxDocumentType.getModel();
        return (String) model.getSelectedItem();
    }

    public void addNewUserListener( ActionListener listener ) {
        jButtonNewUser.addActionListener( listener );
    }

    public void removeNewUserListener( ActionListener listener ) {
        jButtonNewUser.removeActionListener( listener );
    }

    public void addRecoverPasswordListener( ActionListener listener ) {
        jButtonPasswordRecover.addActionListener( listener );
    }

    public void removeRecoverPasswordListener( ActionListener listener ) {
        jButtonPasswordRecover.removeActionListener( listener );
    }

    public void addLoginListener( ActionListener listener ) {
        jButtonLogin.addActionListener( listener );
    }

    public void removeLoginListener( ActionListener listener ) {
        jButtonLogin.removeActionListener( listener );
    }

    public void addDocumentTypeChangeListener( ItemListener listener ) {
        jComboBoxDocumentType.addItemListener( listener );
    }

    public void removeDocumentTypeChangeListener( ItemListener listener ) {
        jComboBoxDocumentType.removeItemListener( listener );
    }

    public void addExitAPPListener( WindowListener listener ){
        this.addWindowListener( listener );
    }

    public void removeExitAPPListener( WindowListener listener ) {
        this.removeWindowListener( listener );
    }

    public void showError( int focuElement, String errorMessage, String titleMessageWindow ) {
        JOptionPane.showMessageDialog( this,
                errorMessage,
                titleMessageWindow,
                JOptionPane.ERROR_MESSAGE);
        switch (focuElement) {
            case DOCUMENT:              jTextFieldDocumentNumber.requestFocus();
            case DOCUMENT_TYPE:         jComboBoxDocumentType.requestFocus();
            case PASSWORD:              jPasswordFieldPassword.requestFocus();
        }
    }

    public Component asWidget() {
        return this;
    }

    public void showOK(int focuElement, String errorMessage, String titleMessageWindow) {
        JOptionPane.showMessageDialog( this,
                errorMessage,
                titleMessageWindow,
                JOptionPane.INFORMATION_MESSAGE);
       
    }
}
